/**
 * 流转与任务结合的业务逻辑
 */
const cloudHelper = require('../../../helpers/cloud_helper.js');
const pageHelper = require('../../../helpers/page_helper.js');

/**
 * 为项目状态创建关联任务
 * @param {String} projectId 项目ID
 * @param {Number} status 项目状态
 * @param {Number} activeStatus 项目活动状态
 * @returns {Promise} 异步结果
 */
async function createTasksForProjectStatus(projectId, status, activeStatus) {
    try {
        let params = {
            projectId,
            status,
            activeStatus
        };
        
        let opt = {
            title: '创建任务中'
        };
        
        let result = await cloudHelper.callCloudData('task/create_tasks_for_state', params, opt);
        return result;
    } catch (err) {
        console.error('创建任务失败', err);
        return null;
    }
}

/**
 * 检查任务完成情况，判断是否可以流转状态
 * @param {String} projectId 项目ID
 * @returns {Promise} 异步结果
 */
async function checkProjectTransitionCondition(projectId) {
    try {
        let params = {
            projectId
        };
        
        let opt = {
            title: '检查任务完成情况'
        };
        
        let result = await cloudHelper.callCloudData('task/check_transition_condition', params, opt);
        return result;
    } catch (err) {
        console.error('检查任务完成情况失败', err);
        return null;
    }
}

/**
 * 更新任务状态
 * @param {String} taskId 任务ID
 * @param {Number} status 任务状态
 * @param {Number} progress 任务进度
 * @returns {Promise} 异步结果
 */
async function updateTaskStatus(taskId, status, progress) {
    try {
        let params = {
            id: taskId,
            status: status
        };
        
        if (progress !== undefined && progress !== null) {
            params.progress = progress;
        }
        
        let opt = {
            title: '更新任务状态'
        };
        
        let result = await cloudHelper.callCloudData('task/update_status', params, opt);
        return result;
    } catch (err) {
        console.error('更新任务状态失败', err);
        return null;
    }
}

/**
 * 获取项目关联任务
 * @param {String} projectId 项目ID
 * @returns {Promise} 异步结果
 */
async function getProjectTasks(projectId) {
    try {
        let params = {
            projectId,
            page: 1,
            size: 20
        };
        
        let opt = {
            title: '加载任务'
        };
        
        let result = await cloudHelper.callCloudData('task/project_tasks', params, opt);
        return result;
    } catch (err) {
        console.error('获取项目任务失败', err);
        return null;
    }
}

/**
 * 获取任务统计数据
 * @param {String} projectId 项目ID
 * @returns {Promise} 异步结果
 */
async function getTaskStats(projectId) {
    try {
        let params = {
            projectId
        };
        
        let opt = {
            title: '加载统计'
        };
        
        let result = await cloudHelper.callCloudData('task/stats', params, opt);
        return result;
    } catch (err) {
        console.error('获取任务统计失败', err);
        return null;
    }
}

module.exports = {
    createTasksForProjectStatus,
    checkProjectTransitionCondition,
    updateTaskStatus,
    getProjectTasks,
    getTaskStats
}; 